package org.jeecg.modules.product.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.modules.product.oa.entity.TbLearningGoal;
import org.jeecg.modules.product.oa.entity.TbLearningGoalRecord;
import org.jeecg.modules.product.oa.mapper.TbLearningGoalMapper;
import org.jeecg.modules.product.oa.mapper.TbLearningGoalRecordMapper;
import org.jeecg.modules.product.oa.service.ITbLearningGoalService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 学习目标
 * @Author: jeecg-boot
 * @Date:   2025-04-17
 * @Version: V1.0
 */
@Slf4j
@Service
public class TbLearningGoalServiceImpl extends ServiceImpl<TbLearningGoalMapper, TbLearningGoal> implements ITbLearningGoalService, Job {

    @Autowired
    private TbLearningGoalMapper tbLearningGoalMapper;
    @Autowired
    private TbLearningGoalRecordMapper tbLearningGoalRecordMapper;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String parameter = String.valueOf(jobExecutionContext.getMergedJobDataMap().get("parameter"));//获取前端配置过来的参数
        log.info("========开始生成计划================",parameter);

        QueryWrapper<TbLearningGoal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("execution_time_type",parameter);
        List<TbLearningGoal> list = tbLearningGoalMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            log.info("========list=================",list);
            TbLearningGoalRecord tbLearningGoalRecord = new TbLearningGoalRecord();
            for (TbLearningGoal tbLearningGoal : list) {
                tbLearningGoalRecord.setGoal(tbLearningGoal.getGoal());
                tbLearningGoalRecord.setType(tbLearningGoal.getType());
                tbLearningGoalRecord.setLearningGoalId(tbLearningGoal.getId());
                tbLearningGoalRecordMapper.insert(tbLearningGoalRecord);
            }
        }
        log.info("========结束生成计划================",parameter);
    }
}
